package com.tanhua.mongo.api;

import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * @Author: cl
 * @Date: 2022/5/6 - 05 - 06
 * @Description:
 */
//互动模块服务(评论点赞)
@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 发布评论并获取最新评论数量(评论点赞喜欢共用接口实现类)
     *
     * @param comment
     * @return
     */
    @Override
    public Integer send(Comment comment) {

        //1、获取动态的userId
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        //2、判断是否存在，(如果存在就设置被评论id)
        if (movement != null) {
            comment.setPublishUserId(movement.getUserId());
        }
        //1.2获取评论的userid
        Comment comment1 = mongoTemplate.findById(comment.getPublishId(), Comment.class);
        if (comment1 != null) {
            comment.setPublishUserId(comment1.getUserId());
        }
        //3、存在保存评论表
        mongoTemplate.save(comment);


        //4、更新动态表
        //4.1 构建条件
        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        //4.2判断是评论还是点赞还是喜欢(判断是动态点赞还是评论点赞)

        if (movement != null) {
            if (comment.getCommentType() == CommentType.LIKE.getType()) {
                //4.3设置某个字段自增加1
                update.inc("likeCount", 1);
            } else if (comment.getCommentType() == CommentType.COMMENT.getType()) {
                update.inc("commentCount", 1);
            } else {
                update.inc("loveCount", 1);
            }
        } else if (comment1 != null) {
            update.inc("likeCount", 1);
        }


        //4.4设置查询前更新为自增后的值
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.upsert(true);
        options.returnNew(true);

        //5、更新动态表字段信息并返回最新数据(判断是动态还是评论)
        if (movement != null) {
            //是动态就更新动态
            Movement movement1 = mongoTemplate.findAndModify(query, update, options, Movement.class);
            return movement1.statisCount(comment.getCommentType());
        } else if (comment1 != null) {
            //是评论就更新评论
            Comment modify = mongoTemplate.findAndModify(query, update, options, Comment.class);
            return modify.getLikeCount();
        }
        return 0;
    }


    /**
     * 根据动态id和评论类型分页查询动态评论信息
     *
     * @param page
     * @param pageSize
     * @param movementId
     * @param comment
     * @return
     */
    @Override
    public List<Comment> findByMovementId(Integer page, Integer pageSize, ObjectId movementId, CommentType comment) {
        //创建条件
        Query query = Query.query(Criteria.where("publishId").is(movementId)
                .and("commentType").is(comment.getType()))
                .skip((page - 1) * pageSize).limit(pageSize)
                .with(Sort.by(Sort.Order.desc("created")));
        //查询数据库
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;
    }

    /**
     * 判断是否点赞或者喜欢
     *
     * @param movementId
     * @param userId
     * @param commentType
     * @return
     */
    @Override
    public boolean isOrNo(ObjectId movementId, Long userId, CommentType commentType) {
        //创建条件
        Query query = Query.query(Criteria.where("publishId").is(movementId)
                .and("userId").is(userId).and("commentType").is(commentType.getType()));
        //查询数据库
        boolean exists = mongoTemplate.exists(query, Comment.class);//exists查询是否存在
        //返回结果
        return exists;
    }

    /**
     * 取消点赞或喜欢
     *
     * @param comment
     * @return
     */
    @Override
    public Integer delete(Comment comment) {
        //1、构建删除条件
        Query query = Query.query(Criteria.where("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType())
                .and("userId").is(comment.getUserId()));
        //2、根据条件删除评论表
        mongoTemplate.remove(query, Comment.class);
        //3、更新动态表
        //3.1 构建条件
        Query query1 = Query.query(Criteria.where("id").is(comment.getPublishId()));

        //判断comment是动态还是评论
        Movement movement1 = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        Comment comment1 = mongoTemplate.findById(comment.getPublishId(), Comment.class);

        Update update = new Update();
        //3.2判断是评论还是点赞还是喜欢

        if (movement1 != null) {
            if (comment.getCommentType() == CommentType.LIKE.getType()) {
                //3.3设置某个字段自增加1
                update.inc("likeCount", -1);
            } else if (comment.getCommentType() == CommentType.COMMENT.getType()) {
                update.inc("commentCount", -1);
            } else {
                update.inc("loveCount", -1);
            }
        } else if (comment1 != null) {
            update.inc("likeCount", -1);
        }


        //3.4设置查询前更新为自增后的值
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.upsert(true);
        options.returnNew(true);


        //4、更新动态表字段信息并返回最新数据
        if (movement1 != null) {
            Movement movement = mongoTemplate.findAndModify(query1, update, options, Movement.class);
            return movement.statisCount(comment.getCommentType());
        } else if (comment1 != null) {
            return mongoTemplate.findAndModify(query1, update, options, Comment.class).getLikeCount();
        }
        return 0;
    }

    /**
     * 根据发布人id和类型查询
     *
     * @param userId
     * @param type
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public List<Comment> getByPublishUserIdAndType(Long userId, Integer type, Integer page, Integer pagesize) {

        Query query = Query.query(Criteria.where("publishUserId").is(userId)
                .and("commentType").is(type))
                .skip((page - 1) * pagesize).limit(pagesize);

        List<Comment> list = mongoTemplate.find(query, Comment.class);

        return list;
    }
}
